package save
{
	import event.UIEditsEventName;

	import fl.controls.Button;
	import fl.controls.Label;
	import fl.controls.TextArea;
	import fl.controls.TextInput;

	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.ByteArray;

	import kingsAsLib.events.EventManager;
	import kingsAsLib.events.KEvents;
	import kingsAsLib.air.KFileDirectory;
	import kingsAsLib.air.KwriterFile;

	/**
	 * @author kings
	 * @E-mail: qywangweiqiang@126.com
	 * 2012-11-30下午04:47:50
	 * */
	public class SaveCodeWins extends Sprite
	{
		static private var saveCode:SaveCodeWins=new SaveCodeWins();

		private var contenLayer:Sprite=new Sprite();
		private var bgLayer:Sprite=new Sprite();
		private var saveASBtn:Button=new Button();
		private var packageText:TextInput=new TextInput();
		private var cancleBtn:Button=new Button();
		private var classNameText:TextInput=new TextInput();
		private var produceBtn:Button=new Button();
		private var browseBtn:Button=new Button();
		private var urlText:TextInput=new TextInput();
		private var classCodeShowText:TextArea=new TextArea();

		public function SaveCodeWins()
		{


			contenLayer.x=0;
			contenLayer.y=0;


			bgLayer.x=0;
			bgLayer.y=0;

			
		 
			bgLayer.graphics.clear();
			bgLayer.graphics.beginFill(0x66CCFF);
			bgLayer.graphics.drawRect(0, 0, 576, 480);
			bgLayer.graphics.endFill();

			saveASBtn.x=238;
			saveASBtn.y=446;
			saveASBtn.width=100;
			saveASBtn.height=22;
			saveASBtn.label='生成as文件'
			bgLayer.addChild(saveASBtn);

			var title_label:Label=new Label();
			title_label.x=209;
			title_label.y=2;
			title_label.width=100;
			title_label.height=22;
			title_label.text='生成as代码'
			bgLayer.addChild(title_label);

			var packageNameLabel:Label=new Label();
			packageNameLabel.x=6;
			packageNameLabel.y=33;
			packageNameLabel.width=100;
			packageNameLabel.height=22;
			packageNameLabel.text='包名'
			bgLayer.addChild(packageNameLabel);


			packageText.x=55;
			packageText.y=32;
			packageText.width=200;
			packageText.height=22;
			packageText.text='';
			bgLayer.addChild(packageText);


			cancleBtn.x=462;
			cancleBtn.y=449;
			cancleBtn.width=100;
			cancleBtn.height=22;
			cancleBtn.label='退出'
			bgLayer.addChild(cancleBtn);

			var classLabel:Label=new Label();
			classLabel.x=312;
			classLabel.y=30;
			classLabel.width=100;
			classLabel.height=22;
			classLabel.text='类名'
			bgLayer.addChild(classLabel);


			classNameText.x=384;
			classNameText.y=31;
			classNameText.width=100;
			classNameText.height=22;
			classNameText.text="";
			bgLayer.addChild(classNameText);

			var codeTxtTitle:Label=new Label();
			codeTxtTitle.x=180;
			codeTxtTitle.y=125;
			codeTxtTitle.width=100;
			codeTxtTitle.height=22;
			codeTxtTitle.text='生成的代码'
			bgLayer.addChild(codeTxtTitle);


			classCodeShowText.x=34;
			classCodeShowText.y=156;
			classCodeShowText.width=500;
			classCodeShowText.height=280;
			classCodeShowText.text="";
			bgLayer.addChild(classCodeShowText);

			produceBtn.x=17;
			produceBtn.y=443;
			produceBtn.width=100;
			produceBtn.height=22;
			produceBtn.label='预览代码'
			bgLayer.addChild(produceBtn);


			browseBtn.x=9;
			browseBtn.y=77;
			browseBtn.width=100;
			browseBtn.height=22;
			browseBtn.label='浏览保存位置'
			bgLayer.addChild(browseBtn);


			urlText.x=126;
			urlText.y=77;
			urlText.width=400;
			urlText.height=22;
			urlText.text="";
			bgLayer.addChild(urlText);

			contenLayer.addChild(bgLayer);
			
			addChild(contenLayer);

			browseBtn.addEventListener(MouseEvent.CLICK, browseHandle);
			produceBtn.addEventListener(MouseEvent.CLICK, produceCodeHandle);
			cancleBtn.addEventListener(MouseEvent.CLICK, cancleHandle);
			saveASBtn.addEventListener(MouseEvent.CLICK, saveHandle);
		}

		static public function instance():SaveCodeWins
		{
			return saveCode;
		}

		protected function cancleHandle(event:MouseEvent):void
		{
			EventManager.dispatchEvent(new KEvents(UIEditsEventName.saveCodeWinShow, false));
		}

		protected function saveHandle(event:MouseEvent):void
		{
			if (urlText.text == "")
			{
				trace("没有保存地址");
				return;
			}
			if (classCodeShowText.text == "")
			{
				trace("没有生成代码");
				return;
			}

			var byt:ByteArray=new ByteArray();
			byt.writeMultiByte(CodeInfo.instance().classAllStr, "utf-8");

			var url:String=urlText.text + "\\" + packageText.text + "\\" + classNameText.text + ".as";
			var kwrite:KwriterFile=new KwriterFile();
			kwrite.saveFile(byt, url);
		}

		protected function browseHandle(event:MouseEvent):void
		{
			var kfdir:KFileDirectory=new KFileDirectory();
			kfdir.browseForDirectory(callback);
			function callback(url:String):void
			{
				urlText.text=url;
			}

		}

		protected function produceCodeHandle(event:MouseEvent):void
		{
			produceCode();
		}


		public function produceCode():void
		{
			if (packageText.text == "")
			{
				trace("没有包头");
				return
			}
			if (classNameText.text == "")
			{
				trace("没有类名 ");
				return
			}
			CodeInfo.instance().packageName=packageText.text;
			CodeInfo.instance().className=classNameText.text;
			var sp:Sprite=UIView.instance().contenLayer;
			DecodeAS.instance().decodeObj(sp);
		}

		public function setCodeStr(str:String):void
		{
			classCodeShowText.text=str;
		}
	}
}
